package com.xu.sso.db.service.impl;

import com.xu.sso.db.dao.base.ApiAuthMapper;
import com.xu.sso.db.model.base.ApiAuth;
import com.xu.sso.db.model.base.ApiAuthExample;
import com.xu.sso.db.service.ApiAuthDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApiAuthDBServiceImpl implements ApiAuthDBService {

    @Autowired
    private ApiAuthMapper apiAuthMapper;

    @Override
    public List<ApiAuth> findAllApiAuth() {
        return apiAuthMapper.selectByExample(new ApiAuthExample());
    }

    @Override
    public ApiAuth findApiAuthId(Integer id) {
        return apiAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public ApiAuth findApiAuthByAppId(String appId) {
        ApiAuthExample example = new ApiAuthExample();
        example.createCriteria().andAppIdEqualTo(appId);
        List<ApiAuth> apiAuths = apiAuthMapper.selectByExample(example);
        if (null != apiAuths && apiAuths.size() > 0) {
            return apiAuths.get(0);
        }
        return null;
    }

    @Override
    public int createApiAuth(ApiAuth apiAuth) {
        apiAuth.setCreateTime(new Date());
        apiAuth.setUpdateTime(new Date());
        return apiAuthMapper.insertSelective(apiAuth);
    }

    @Override
    public int updateApiAuth(ApiAuth apiAuth) {
        apiAuth.setUpdateTime(new Date());
        return apiAuthMapper.updateByPrimaryKeySelective(apiAuth);
    }

    @Override
    public int updateApiAppName(String appId, String appName) {
        ApiAuthExample example = new ApiAuthExample();
        example.createCriteria().andAppIdEqualTo(appId);
        ApiAuth apiAuth = new ApiAuth();
        apiAuth.setAppName(appName);
        return apiAuthMapper.updateByExampleSelective(apiAuth, example);
    }

    @Override
    public int updateApiAuthStatus(Integer id, String status) {
        ApiAuth apiAuth = new ApiAuth();
        apiAuth.setId(id);
        apiAuth.setStatus(status);
        apiAuth.setUpdateTime(new Date());
        return apiAuthMapper.updateByPrimaryKeySelective(apiAuth);
    }

    @Override
    public int deleteApiAuth(Integer id) {
        return apiAuthMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteApiAuthByAppId(String appId) {
        ApiAuthExample example = new ApiAuthExample();
        example.createCriteria().andAppIdEqualTo(appId);
        return apiAuthMapper.deleteByExample(example);
    }

}
